package com.saunter.controller;

import com.saunter.model.User;
import com.saunter.service.user.AdminService;
import com.saunter.utils.exception.ExceptionManager;
import com.saunter.utils.response.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

@Api(tags = {"管理员编辑用户"})
@RestController
@RequestMapping("/admin")
public class AdminUserController {
    @Autowired
    private AdminService adminService;
    @Resource
    private ExceptionManager exceptionManager;

    @ApiOperation("添加用户")
    @PostMapping("/addUser")
    public Map<String, Object> addUser(@RequestParam("id_num")String id_num, @ApiParam(hidden = true)@RequestParam(value = "passwd", required = false)String passwd, @RequestParam("name")String name, @RequestParam("birthdate")long birthdate, @ApiParam(value = "1:男;2:女", required = true) @RequestParam("sex")int sex, @RequestParam("job_id")int job_id, @RequestParam(value = "phone", required = false)String phone, @RequestParam(value = "email", required = false)String email, @ApiParam(value = "0:离职;1:在职", defaultValue = "1")@RequestParam(value = "status", required = false)Integer status, @RequestParam(value = "remark", defaultValue = "")String remark) throws Exception {
        if(1 == adminService.addUser(new User(id_num, passwd, name, new Date(birthdate), sex, job_id, phone, email, status, new Date(System.currentTimeMillis()), remark))) {
            return Response.success("添加成功");
        }else {
            throw exceptionManager.create("Sql002");
        }
    }

    @ApiOperation("查看用户")
    @GetMapping("/userList/{page_no}/{page_size}")
    public Map<String, Object> userList(@PathVariable("page_no")int page_no, @PathVariable("page_size")int page_size,
                                        @RequestParam(value = "name", required = false)String filter_name, @RequestParam(value = "id_num", required = false)String filter_id_num, @RequestParam(value = "sex", required = false)Integer filter_sex, @RequestParam(value = "department_id", required = false)String filter_department_id, @RequestParam(value = "job_id", required = false)String filter_job_id, @ApiParam(value = "0:离职;1:在职")@RequestParam(value = "status", required = false)String filter_status,
                                        @ApiParam("name asc/desc") @RequestParam(value = "order_by", required=false)String order_by) {
        Map<String, Object> filters = new HashMap<String, Object>();
        filters.put("name", filter_name);
        filters.put("id_num", filter_id_num);
        filters.put("sex", filter_sex);
        filters.put("department_id", filter_department_id);
        filters.put("job_id", filter_job_id);
        filters.put("status", filter_status);
        filters.put("order_by", order_by);

        return Response.success(adminService.getUsersAndJob(page_no, page_size, filters));
    }

    @ApiOperation("修改用户")
    @PostMapping("/editUser")
    public Map<String, Object> editUser (@RequestParam("id_num")String id_num, @RequestParam(value="name", required = false)String name,
                                         @RequestParam(value="job_id", required = false)Integer job_id, @ApiParam("0:离职;1:在职")@RequestParam(value="status", required = false)Integer status,
                                         @RequestParam(value="phone", required = false)String phone, @RequestParam(value="email", required = false)String email, @RequestParam(value="remark", required = false)String remark) {
        if(0 != adminService.editUser(new User(id_num, null, name, null, null, job_id, phone, email, status, null, remark))) {
            return Response.success("修改成功");
        }else {
            throw exceptionManager.create("Sql002");
        }
    }

    @ApiOperation("导出用户")
    @GetMapping("/exportUser")
    public void exportUser(HttpServletResponse response, @RequestParam(value = "name", required = false)String filter_name, @RequestParam(value = "id_num", required = false)String filter_id_num, @RequestParam(value = "sex", required = false)Integer filter_sex, @RequestParam(value = "department_id", required = false)String filter_department_id, @RequestParam(value = "job_id", required = false)String filter_job_id, @ApiParam("0:离职;1:在职")@RequestParam(value = "status", required = false)String filter_status,
                           @RequestParam(value = "order_by", required=false)String order_by) throws Exception {
        Map<String, Object> filters = new HashMap<String, Object>();
        filters.put("name", filter_name);
        filters.put("id_num", filter_id_num);
        filters.put("sex", filter_sex);
        filters.put("department_id", filter_department_id);
        filters.put("job_id", filter_job_id);
        filters.put("status", filter_status);
        filters.put("order_by", order_by);
        adminService.exportUsers(response, filters);
    }

    @ApiOperation("导入用户")
    @PostMapping("importUser")
    public Map<String, Object> importUser(@RequestParam("user_excel") MultipartFile file, @RequestParam("job_id")Integer job_id) throws IOException {
        if(file.isEmpty()) {
            throw exceptionManager.create("File001");
        }
        if(!file.getContentType().contains("sheet")) {
            throw exceptionManager.create("File002");
        }
        if(500 * 1024 < file.getSize()){
            throw exceptionManager.create("File003");
        }
        adminService.importUsers(file, job_id);
        return Response.success("上传成功");
    }

    @ApiOperation("下载用户导入模板")
    @GetMapping("getUserExcelTemplate")
    public void getUserExcelTemplate(HttpServletResponse response) throws Exception {
        adminService.getUserExcelTemplate(response);
    }

    @ApiOperation("删除用户")
    @GetMapping("/deleteUser/{id_num}")
    public Map<String, Object> deleteUser(@PathVariable("id_num")String id_num){
        if(1 == adminService.deleteUser(id_num)) {
            return Response.success("修改成功");
        }else {
            throw exceptionManager.create("Sql002");
        }
    }

    @ApiOperation("重置用户密码用户")
    @GetMapping("/resetUserPasswd/{id_num}")
    public Map<String, Object> resetUserPasswd(@PathVariable("id_num")String id_num){
        if(1 == adminService.resetUserPasswd(id_num)) {
            return Response.success("修改成功");
        }else {
            throw exceptionManager.create("Sql002");
        }
    }
}
